<?php
/**
 * Radcodes - SocialEngine Module
 *
 * @category   Application_Extensions
 * @package    Story
 * @copyright  Copyright (c) 2009-2010 Radcodes LLC (http://www.radcodes.com)
 * @license    http://www.radcodes.com/license/
 * @version    $Id$
 * @author     Vincent Van <vincent@radcodes.com>
 */
?>
<!-- FOLLOW -->
<style type="text/css">
.stories_follow .story_radio_loading
{
     background-image: url(application/modules/Core/externals/images/loading.gif);
     background-repeat: no-repeat;
     overflow: hidden;
}
.stories_follow .story_radio_loading a
{
     visibility: hidden;
}
</style>
<!-- FOLLOW -->

<?php if ($this->paginator->getTotalItemCount()): ?>
  <?php if ($this->display_style == 'narrow'): ?>

    <ul class='stories_list'>
      <?php foreach ($this->paginator as $story): ?>
        <li>
          <?php if ($this->showphoto): ?>
            <div class="story_photo">
              <?php echo $this->htmlLink($story->getHref(), $this->itemPhoto($story, 'thumb.icon'));?>
            </div>
          <?php endif; ?>
          <div class="story_content">
            <div class="story_title">
              <?php echo $this->htmlLink($story->getHref(), $this->radcodes()->text()->truncate($story->getTitle(), 28)) ?>
            </div>
            <?php if ($this->showmeta): ?>
                <?php 
                  $meta_options = array('story' => $story,
                    'display_style' => 'narrow',
                    'show_comments' => $this->order == 'mostcommented' ? true : false,
                    'show_views' => $this->order == 'mostviewed' ? true : false,
                    'show_likes' => $this->order == 'mostliked' ? true : false,
                  );
                  
                  if (in_array($this->order, array('mostcommented','mostviewed','mostliked'))) {
                    $meta_options['show_owner'] = false;
                    $meta_options['show_date'] = false;
                  }
                  
                ?>
              <div class="story_meta">  
                <?php echo $this->partial('index/_meta.tpl', 'story', $meta_options)?>
              </div>
            <?php endif; ?>
            <!-- FOLLOW -->
            <?php if ($this->viewer_id): ?>
            <script type="text/javascript">
                en4.core.runonce.add(function(){
                     $$('#follow_options<?php echo $story->story_id; ?> a').addEvent('click', function(){
                          var option_id = this.get('data');
                          $$('#follow_options<?php echo $story->story_id; ?> .story_profile_tools').each(function(row){
                            row.className= 'story_radio_loading';
                            });
                          new Request.JSON({
                              url: '<?php echo $this->url(array('module' => 'story', 'controller' => 'widget', 'action' => 'story-follow'), 'default', true); ?>',
                              method: 'post',
                              data : {
                                format: 'json',
                                  'story_id': <?php echo $story->story_id; ?>,
                                  'option_id' : option_id
                              },
                              onComplete: function(responseJSON, responseText) {
                                  if (responseJSON.error) {
                                    alert(responseJSON.error);
                                   }else{
                                    $$('#follow_options<?php echo $story->story_id; ?> .story_radio_loading').each(function(row){
                                        row.className= 'story_profile_tools';
                                        });
                                     var text_follow = '<?php echo $this->translate('Follow'); ?>'; 
                                     var data_follow = 1;
                                     if(option_id==1){
                                      text_follow = '<?php echo $this->translate('Unfollow'); ?>'; 
                                      data_follow = 0;
                                     } 
                                     $$('#follow_options<?php echo $story->story_id; ?> .story_profile_tools a').each(function(row){
                                        row.setAttribute("data", data_follow);
                                       });
                                      $$('#follow_options<?php echo $story->story_id; ?> .story_profile_tools a').each(function(row){
                                        row.innerHTML = text_follow;
                                       });
                                   }
                               }
                          }).send();
                     });
                });
           </script>
            <?php $follow = $story->getFollow(array('story_id'=>$story->story_id,'user_id'=>$this->viewer_id)); ?>
              <div class="stories_follow" id="follow_options<?php echo $story->story_id; ?>">
                <?php if ($follow == 1): ?>
                <div class="story_profile_tools">
                    <a href="javascript:;" data="0" class="buttonlink icon_story_viewall "><?php echo $this->translate('Unfollow'); ?></a>
                </div>
                <?php endif; ?>
                <?php if ($follow == 0): ?>
                <div class="story_profile_tools">
                    <a href="javascript:;" data="1" class="buttonlink icon_story_viewall "><?php echo $this->translate('Follow'); ?></a>
                </div>
                <?php endif; ?>
              </div>
            <?php endif; ?>
            <!-- FOLLOW -->

            <?php if ($this->showdescription && $story->getDescription()): ?>
              <div class="story_description">
                <?php echo $this->viewMore($story->getDescription()); ?>
              </div>
            <?php endif; ?>
          </div>
        </li>
      <?php endforeach; ?>  
    </ul>

  <?php else: ?>
    <ul class='stories_rows'>
      <?php foreach ($this->paginator as $story): ?>
        <li>
          <?php if ($this->showphoto): ?>
            <div class="story_photo">
              <?php echo $this->htmlLink($story->getHref(), $this->itemPhoto($story, 'thumb.normal'));?>
            </div>
          <?php endif; ?>
          <div class="story_content">
            <div class="story_title">
              <?php echo $this->partial('index/_title.tpl', 'story', array('story' => $story))?>
            </div>

            <!-- FOLLOW -->
            <?php if ($this->viewer_id): ?>
            <script type="text/javascript">
                en4.core.runonce.add(function(){
                     $$('#follow_options<?php echo $story->story_id; ?> a').addEvent('click', function(){
                          var option_id = this.get('data');
                          $$('#follow_options<?php echo $story->story_id; ?> .story_profile_tools').each(function(row){
                            row.className= 'story_radio_loading';
                            });
                          new Request.JSON({
                              url: '<?php echo $this->url(array('module' => 'story', 'controller' => 'widget', 'action' => 'story-follow'), 'default', true); ?>',
                              method: 'post',
                              data : {
                                format: 'json',
                                  'story_id': <?php echo $story->story_id; ?>,
                                  'option_id' : option_id
                              },
                              onComplete: function(responseJSON, responseText) {
                                  if (responseJSON.error) {
                                    alert(responseJSON.error);
                                   }else{
                                    $$('#follow_options<?php echo $story->story_id; ?> .story_radio_loading').each(function(row){
                                        row.className= 'story_profile_tools';
                                        });
                                     var text_follow = '<?php echo $this->translate('Follow'); ?>'; 
                                     var data_follow = 1;
                                     if(option_id==1){
                                      text_follow = '<?php echo $this->translate('Unfollow'); ?>'; 
                                      data_follow = 0;
                                     } 
                                     $$('#follow_options<?php echo $story->story_id; ?> .story_profile_tools a').each(function(row){
                                        row.setAttribute("data", data_follow);
                                       });
                                      $$('#follow_options<?php echo $story->story_id; ?> .story_profile_tools a').each(function(row){
                                        row.innerHTML = text_follow;
                                       });
                                   }
                               }
                          }).send();
                     });
                });
           </script>
            <?php $follow = $story->getFollow(array('story_id'=>$story->story_id,'user_id'=>$this->viewer_id)); ?>
              <div class="stories_follow" id="follow_options<?php echo $story->story_id; ?>">
                <?php if ($follow == 1): ?>
                <div class="story_profile_tools">
                    <a href="javascript:;" data="0" class="buttonlink icon_story_viewall "><?php echo $this->translate('Unfollow'); ?></a>
                </div>
                <?php endif; ?>
                <?php if ($follow == 0): ?>
                <div class="story_profile_tools">
                    <a href="javascript:;" data="1" class="buttonlink icon_story_viewall "><?php echo $this->translate('Follow'); ?></a>
                </div>
                <?php endif; ?>
              </div>
            <?php endif; ?>
            <!-- FOLLOW -->

            <?php if ($this->showmeta): ?>
              <div class="story_meta">
                <?php echo $this->partial('index/_meta.tpl', 'story', array('story' => $story))?>
              </div>  
            <?php endif; ?>

            <?php if ($this->showdescription && $story->getDescription()): ?>
              <div class="story_description">
                <?php echo $this->viewMore($story->getDescription()); ?>
              </div>
            <?php endif; ?>
          </div>
        </li>
      <?php endforeach; ?>  
    </ul>

  <?php endif;?>
  
<?php else: ?>
  <div class="tip">
    <span>
      <?php echo $this->translate('There are no stories posted yet.');?>
    </span>
  </div>    
  
<?php endif; ?>

